/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Golf_Manager.model;

import Golf_Manager.vo.JoinMember;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Administrator
 */
public class JoinMemberModel extends AbstractTableModel { 
    
    
    Class[] types = new Class [] { java.lang.Boolean.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class };
    private List<Golf_Manager.vo.JoinMember> joinMembers = new ArrayList<>();
    private String[] columns = new String [] {"선택", "조이름", "이름", "생년월일", ""};
    private boolean[] canEdit = new boolean [] { true, false, false, false, false };
    private int checkRow = -1;
    private Boolean checkValue = false;
    
    public JoinMemberModel() {
        
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return types[columnIndex];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit[columnIndex];
    }

    @Override
    public int getRowCount() {
        return joinMembers.size();
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }
    
    @Override
    public String getColumnName(int column) {
        return columns[column];
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch(columnIndex){
            case 0: {
                if(this.getCheckRow() != rowIndex){
//                    System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " this.checkRow != rowIndex : false");
                    return false;
                } else {
//                    System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " checkValue : " + checkValue);
                    return getCheckValue();
                }
            }
            case 1: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " getName : " + joinMembers.get(rowIndex).getName());
                return joinMembers.get(rowIndex).getGroupNm();
            }
            case 2: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " getName : " + joinMembers.get(rowIndex).getName());
                return joinMembers.get(rowIndex).getName();
            }
            case 3: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " getBirth : " + joinMembers.get(rowIndex).getBirth());
                return joinMembers.get(rowIndex).getBirth();
            }
            case 4: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " getSelectKey : " + joinMembers.get(rowIndex).getSelectKey());
                return joinMembers.get(rowIndex).getSelectKey();
            }
            default: {
//                System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " default : " + null);
                return null;
            }
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
//        System.out.println("rowIndex : " + rowIndex + " columnIndex : " + columnIndex + " aValue : " + aValue);
        switch(columnIndex){
            case 0: {
                if(this.getCheckRow() != rowIndex){
                    this.setCheckRow(rowIndex);
                    this.setCheckValue((Boolean)aValue);
                } else {
                    if((Boolean)aValue == false){
                        this.setCheckRow(-1);
                        this.setCheckValue(false);
                    } else {
                        this.setCheckRow(rowIndex);
                        this.setCheckValue((Boolean)aValue);
                    }
                }
            }
        }
        fireTableDataChanged();
    }

    public List<JoinMember> getJoinMembers() {
        return joinMembers;
    }

    public void setJoinMembers(List<JoinMember> joinMembers) {
        this.joinMembers = new ArrayList<>(joinMembers);
        fireTableDataChanged();
    }
    
    public JoinMember getJoinMember(int rowIndex){
        return this.joinMembers.get(rowIndex);
    }
    
    public void removeJoinMember(int rowIndex){
        this.joinMembers.remove(rowIndex);
        fireTableDataChanged();
    }
    
    public int getCheckRow() {
        return checkRow;
    }

    public void setCheckRow(int checkRow) {
        this.checkRow = checkRow;
    }

    public Boolean getCheckValue() {
        return checkValue;
    }

    public void setCheckValue(Boolean checkValue) {
        this.checkValue = checkValue;
    }
    
    public void setGroupData(int rowIndex, String groupNm, int groupRow, int groupCol){
        this.joinMembers.get(rowIndex).setGroupNm(groupNm);
        this.joinMembers.get(rowIndex).setGroupRow(groupRow);
        this.joinMembers.get(rowIndex).setGroupCol(groupCol);
        fireTableDataChanged();
    }
}
